﻿using System.IO;
using Flexible.Interfaces;

namespace Flexible.Framework
{
    class SettingsStreamer : ISettingsStreamer
    {
        string settingsRootDirectory;

        public SettingsStreamer(string settingsRootDirectory)
        {
            this.settingsRootDirectory = settingsRootDirectory;
        }

        public Stream GetReadStream(IPluginSignature signature)
        {
            string file = GetPluginSettingFilePath(signature);
            if (!File.Exists(file))
                return Stream.Null;
            return File.OpenRead(file);
        }

        private string GetPluginSettingFilePath(IPluginSignature signature)
        {
            return Path.Combine(settingsRootDirectory, signature.Id);
        }

        public Stream GetWriteStream(IPluginSignature signature)
        {
            return File.OpenWrite(GetPluginSettingFilePath(signature));
        }
    }
}
